***************************************************************************
* ShockSeries merged with MacroData
***************************************************************************

local shockpath = "${projectdir}/NarrativeShocks"


global mute1945 = 1  // drop 1945 budget when contructing shocks

// Spending first
import excel using "`shockpath'/SpendShocks", sheet("spending") cellrange(:K404) firstrow clear

replace Date = yq(year(Date), quarter(Date))
format Date %tq

tempfile SpendData
save `SpendData'	

// load narrative tax shocks
if $mute1945 == 1 {
	
if "$Unanticipated" == "1" & "$noITC" == "0" { 
import excel using "`shockpath'/TaxShocksRobustness.xlsx", sheet("unanticipated") cellrange(:C404) firstrow clear
}

if "$noITC" == "1" & "$Unanticipated" == "0" { 
import excel using "`shockpath'/TaxShocksRobustness.xlsx", sheet("baseline_NoITC") cellrange(:C404) firstrow clear 
}

if "$noITC" == "0" & "$Unanticipated" == "0"  & "$shocksheet" == "alternative"  { 	
import excel using "`shockpath'/TaxShocksRobustness.xlsx", sheet("alternative") cellrange(:C404) firstrow clear 
}

if "$noITC" == "0" & "$Unanticipated" == "0"  & "$shocksheet" == "baseline"  { 	
import excel using "`shockpath'/TaxShocksBaseline.xlsx", sheet("baseline") cellrange(:C404) firstrow clear 
}

}

replace Date = yq(year(Date), quarter(Date))
format Date %tq

if $mute1945 == 0 {
	replace X_TaxToGDP = 0 if inrange(Date, tq(1945q1), tq(1947q4))
}


// QE Outlier Check 2009q2 2012q1 2012q2 2012q4 2016q4

if $QEOutlier == 1 {
	
	replace X_TaxToGDP = 0 if inrange(Date, tq(2009q2), tq(2010q2))
	replace X_TaxToGDP = 0 if inrange(Date, tq(2012q1), tq(2013q4))
	replace X_TaxToGDP = 0 if inrange(Date, tq(2016q4), tq(2017q4))
}

// Pre-QE Outlier Check 2009q2 2012q1 2012q2 2012q4 2016q4
// March 2009, October 2011, February 2012, July 2012, August 2016.

if $QEOutlier == 2 {
	
	replace X_TaxToGDP = 0 if inrange(Date, tq(2007q4), tq(2008q4))
	replace X_TaxToGDP = 0 if inrange(Date, tq(2010q3), tq(2012q2))
	replace X_TaxToGDP = 0 if inrange(Date, tq(2015q2), tq(2016q2))
}


* Drop largest shocks

if $DropLargeShock == 1 {
	replace X_TaxToGDP = 0 if Date == tq(1933q2)
}

if $DropLargeShock == 2 {
	replace X_TaxToGDP = 0 if Date == tq(1935q2)
}

if $DropLargeShock == 3 {
	replace X_TaxToGDP = 0 if Date == tq(1947q1)
}

if $DropLargeShock == 4 {
	replace X_TaxToGDP = 0 if Date == tq(1947q2)
}

if $DropLargeShock == 5 {
	replace X_TaxToGDP = 0 if Date == tq(2010q2)
}

if $DropLargeShock == 6 {
	replace X_TaxToGDP = 0 if Date == tq(2016q2)
}

	
gen X_TaxcutToGDP = -X_TaxToGDP     // adjust the sign

merge 1:1 Date using ${datapath}/MacroData, nogen 

merge 1:1 Date using `SpendData', nogen 

replace X_TaxcutToGDP = 0 if Date < tq(1920q1)

drop if missing(X_TaxcutToGDP)
tsset Date, quarterly

if "${shocksheet}" == "baseline" {
	global DataSetName = "MacroData_${shockversion}Shocks"
}
else {
	global DataSetName = "MacroData_${shockversion}Shocks_${shocksheet}"
}    

gen dummyWWII = 0
replace dummyWWII = 1 if inrange(Date,  tq(${skip_start}q1), tq(${skip_end}q4) )

* ATR outliers
sum ATR if inrange(Date,  tq(${sample_start}), tq(${sample_end}) ), det
gen ATRdummies = 1 if (ATR > `r(p99)' | ATR < `r(p1)') & ATR ! = . & inrange(Date,  tq(${sample_start}), tq(${sample_end}) )
gen temp = _n
replace ATRdummies = ATRdummies * temp
replace ATRdummies = 0 if ATRdummies == .
drop temp


* Create Regimes

if $RegimeDef == 1 {
	
	gen Regime = 0
	replace Regime = 1 if BankRate == 2
	replace Regime = 1 if BankRate <= 0.5
	replace Regime = 1 if Date == tq(1939q3)
			
	forvalues h = 0/$hmax {
		
		gen 	Regime`h' = 0
	    replace Regime`h' = 1 if f${ForceZLB}.l.Regime == 1
	    replace Regime`h' = 0 if l.Regime == 0
	}
	
}
	
else if $RegimeDef == 2 {
	
	gen Regime = 0
	replace Regime = 1 if BankRate <= 0.5
	replace Regime = 1 if Date == tq(1939q3)
	replace Regime = 1 if Date >= tq(1932q2) &  Date <= tq(1939q4)
	
	forvalues h = 0/$hmax {
		
		gen 	Regime`h' = 0
	    replace Regime`h' = 1 if f${ForceZLB}.l.Regime == 1
		replace Regime`h' = 0 if l.Regime == 0
	}
	
}


	else if $RegimeDef == 3 {
	
		gen Regime = 0
		replace Regime = 1 if BankRate <= 0.5
		replace Regime = 1 if Date >= tq(1945q2) &  Date <= tq(1951q3)	
	
	forvalues h = 0/$hmax {
		
		gen 	Regime`h' = 0
	    replace Regime`h' = 1 if f${ForceZLB}.l.Regime == 1
		replace Regime`h' = 0 if l.Regime == 0
	}	
}


	else if $RegimeDef == 4 {
	
	gen Regime = 0
	replace Regime = 1 if BankRate == 2
	replace Regime = 1 if BankRate <= 0.5
	replace Regime = 1 if Date == tq(1939q3)
		
	forvalues h = 0/$hmax {
		
		gen 	Regime`h' = 0
		replace Regime`h' = 1 if f${ForceZLB}.Regime == 1
		replace Regime`h' = 0 if Regime == 0
	    
	}	
}


save ${datapath}/${DataSetName}, replace
